local skill = fk.CreateSkill {
  name = "sanshe__enemy_at_the_gates_skill",
}

Fk:loadTranslationTable{
  ["sanshe__enemy_at_the_gates_skill"] = "兵临城下",
  ["#sanshe__enemy_at_the_gates_skill"] = "选择一名其他角色，你展示牌堆顶四张牌，依次对其使用其中【杀】，其余牌放回牌堆顶",
}

skill:addEffect("cardskill", {
  prompt = "#sanshe__enemy_at_the_gates_skill",
  can_use = Util.CanUse,
  target_num = 1,
  mod_target_filter = function(self, player, to_select, selected)
    return to_select ~= player
  end,
  target_filter = Util.CardTargetFilter,
  on_effect = function(self, room, effect)
    local player = effect.from
    local target = effect.to

    effect.extra_data = effect.extra_data or {}
    effect.extra_data.ids = effect.extra_data.ids or {} --指定展示的牌
    local n,cards
    if #effect.extra_data.ids ~= 0 then
      cards = effect.extra_data.ids
    else
      n = 4 + (effect.extra_data.num or 0)--负值可展示4以下张牌?
      if n < 1 then return end
      cards = room:getNCards(n, effect.extra_data.fromArea or "top")
    end
    effect.extra_data.EXenemy_at_the_gates = cards
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, skill.name, nil, true, player)
    cards = effect.extra_data.EXenemy_at_the_gates
    local rest = {}
    while not target.dead do
      cards = table.filter(cards, function (id)
        return room:getCardArea(id) == Card.Processing
      end)
      if #cards == 0 then break end
      room:delay(math.min(#cards,9) * 100)
      local card = Fk:getCardById(cards[1])
      if card.trueName == "slash" and player:canUseTo(card, target, {bypass_distances = true, bypass_times = true}) then
        room:useCard({
          card = card,
          from = player,
          tos = {target},
          extraUse = true,
        })
      else
        table.insert(rest, cards[1])
        table.remove(cards, 1)
      end
    end
    rest = table.filter(rest, function (id)
      return room:getCardArea(id) == Card.Processing
    end)
    if #rest > 0 then
      local toArea = effect.extra_data.toArea
      if toArea == "top" then
        room:moveCards({
          ids = table.reverse(rest), toArea = Card.DrawPile, moveReason = fk.ReasonPut,
        })
      elseif toArea == "bottom" then
        room:moveCards({
          ids = rest, toArea = Card.DrawPile, moveReason = fk.ReasonPut, drawPilePosition = -1,
        })
      else
        room:moveCards({
          ids = rest,
          toArea = Card.DiscardPile,
          moveReason = fk.ReasonPutIntoDiscardPile,
        })
      end
    end
  end,
})

return skill
